package cn.mumzone.pos.model.source;

/**
 * Created by qiuzhenhuan on 2019/6/24.
 */


import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.source.AbsRepository;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.RxUtil;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.model.AppVersionEntity;
import cn.mumzone.pos.model.BannerResult;
import cn.mumzone.pos.model.HelpCenterEntity;
import cn.mumzone.pos.model.ShopConfigEntity;
import cn.mumzone.pos.model.SunMiPayRequestEntity;
import retrofit2.Response;

public class AppRepository extends AbsRepository {


    //开启排班
    private static final String SP_KEY_OPEN_SCHEDULE = "open_schedule";
    //打印交班小票
    private static final String SP_KEY_PRINT_OFF_WORK_TICKET = "print_off_work_ticket";
    //显示客显
    private static final String SP_KEY_GUEST_DISPLAY = "guest_display";
    //小票打印次数
    private static final String SP_KEY_PRINT_COUNT = "print_count";

    //显示门店电话
    private static final String SP_KEY_SHOW_SHOP_PHONE = "show_shop_phone";
    //显示门店地址
    private static final String SP_KEY_SHOW_SHOP_ADDRESS = "show_shop_address";
    //显示购买须知
    private static final String SP_KEY_SHOW_BUY_NOTE = "show_buy_note";
    //显示会员信息
    private static final String SP_KEY_SHOW_MEMBER_INFO = "show_member_info";

    //购买须知
    private static final String SP_KEY_BUY_NOTE = "buy_note";


    //商米支付回调失败请求
    private static final String SP_KEY_SUN_MI_PAY_CALLBACK_ERROR_URL = "sunmi_pay_callback_error_url";

    public void saveBaseUrl(String baseUrl) {

        mSPManager.put("base_url", baseUrl);

    }


    public void updateCheck(ApiCallBack<AppVersionEntity> callBack) {
        ApiRequest.getAppService()
                .updateCheck()
                .compose(RxUtil.<Response<BaseResponse<AppVersionEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    public void findPostersListPage(ApiCallBack<BannerResult> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("status", "0");
        requestMap.put("validity", "0");
        ApiRequest.getAppService()
                .findPostersListPage(requestMap)
                .compose(RxUtil.<Response<BaseResponse<BannerResult>>>applySchedulers())
                .subscribe(callBack);

    }

    public void getHelpCenterData(ApiCallBack<HelpCenterEntity> callBack) {
        ApiRequest.getAppService()
                .getHelpCenterData()
                .compose(RxUtil.<Response<BaseResponse<HelpCenterEntity>>>applySchedulers())
                .subscribe(callBack);
    }

    public ShopConfigEntity getAppConfig() {
        ShopConfigEntity config = new ShopConfigEntity();
        config.setOpenSchedule(mSPManager.getBoolean(SP_KEY_OPEN_SCHEDULE, false));
        config.setPrintOffWorkTicket(mSPManager.getBoolean(SP_KEY_PRINT_OFF_WORK_TICKET, false));
        config.setShowGuestDisplay(mSPManager.getBoolean(SP_KEY_GUEST_DISPLAY, false));
        config.setPrintCount(mSPManager.getInt(SP_KEY_PRINT_COUNT, 2));
        config.setShowShopPhone(mSPManager.getBoolean(SP_KEY_SHOW_SHOP_PHONE, false));
        config.setShowShopAddress(mSPManager.getBoolean(SP_KEY_SHOW_SHOP_ADDRESS, false));
        config.setShowBuyNote(mSPManager.getBoolean(SP_KEY_SHOW_BUY_NOTE, false));
        config.setShowMemberInfo(mSPManager.getBoolean(SP_KEY_SHOW_MEMBER_INFO, false));
        config.setButNote(mSPManager.getString(SP_KEY_BUY_NOTE));
        return config;
    }

    public void setAppConfig(boolean isOpenSchedule, boolean isPrintOffWorkTicket, boolean isShowGuestDisplay, int printCount,
                             boolean isShowShopPhone, boolean isShowShopAddress, boolean isShowBuyNote, boolean isShowMemberInfo,
                             String butNote) {
        mSPManager.put(SP_KEY_OPEN_SCHEDULE, isOpenSchedule);
        mSPManager.put(SP_KEY_PRINT_OFF_WORK_TICKET, isPrintOffWorkTicket);
        mSPManager.put(SP_KEY_GUEST_DISPLAY, isShowGuestDisplay);
        mSPManager.put(SP_KEY_PRINT_COUNT, printCount);
        mSPManager.put(SP_KEY_SHOW_SHOP_PHONE, isShowShopPhone);
        mSPManager.put(SP_KEY_SHOW_SHOP_ADDRESS, isShowShopAddress);
        mSPManager.put(SP_KEY_SHOW_BUY_NOTE, isShowBuyNote);
        mSPManager.put(SP_KEY_SHOW_MEMBER_INFO, isShowMemberInfo);
        mSPManager.put(SP_KEY_BUY_NOTE, butNote);

        AppConstants.CONFIG_OPEN_SCHEDULE = isOpenSchedule;
        AppConstants.CONFIG_PRINT_OFF_WORK_TICKET = isPrintOffWorkTicket;
        AppConstants.CONFIG_GUEST_DISPLAY = isShowGuestDisplay;
        AppConstants.CONFIG_PRINT_COUNT = printCount;
        AppConstants.CONFIG_SHOW_SHOP_PHONE = isShowShopPhone;
        AppConstants.CONFIG_SHOW_SHOP_ADDRESS = isShowShopAddress;
        AppConstants.CONFIG_SHOW_BUY_NOTE = isShowBuyNote;
        AppConstants.CONFIG_SHOW_MEMBER_INFO = isShowMemberInfo;

    }

    public String getBuyNote() {
        return mSPManager.getString(SP_KEY_BUY_NOTE);
    }

    public void saveRemoteVersion(String version) {
        mSPManager.put(AppConstants.VERION_NAME, version);
    }


    public String getRemoteVersion() {
        return mSPManager.getString(AppConstants.VERION_NAME);
    }

}
